package com.mtiiot.fsmanage.controller;

import com.mtiiot.fsmanage.server.SysServer;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;


@SuppressWarnings("all")
@Controller
public class SysController {
	@Autowired
    private SysServer sysServer;
    /**
     * add
     * @return
     */
    @RequestMapping(value="/sys/add.action",produces="application/text;charset=UTF-8")
    @ResponseBody
    public String add(String sysName,String desc,String sysId) throws Exception{
        boolean bool=sysServer.validateSysName(sysName);
        if(bool){
            return "sysNameExist";
        }
        if(!"".equals(sysId)){
            //手动输入id
            bool=sysServer.validateSysId(sysId);
            if(bool){
                return "sysIdExist";
            }
        }

        bool=sysServer.addSys(sysName,desc,sysId);
        if(bool){
            return "success";
        }
        return "failure";
    }

    /**
     * query
     * @return
     */
    @RequestMapping(value="/sys/query.action",produces="application/json;charset=UTF-8")
    @ResponseBody
    public Object query(String keyWrod)  throws Exception{
        Object obj=sysServer.querySys(keyWrod);
        ObjectMapper mapper = new ObjectMapper();
        String jsonStr= null;
        try {
            jsonStr = mapper.writeValueAsString(obj);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return jsonStr;
    }

    /**
     * update
     * @return
     */
    @RequestMapping(value="/sys/update.action",produces="application/text;charset=UTF-8")
    @ResponseBody
    public String update(String id,String sysName,String desc) throws Exception{
        boolean bool=sysServer.validateSysName(sysName);
        if(bool){
            return "sysNameExist";
        }
        bool=sysServer.updateSys(id,sysName,desc);
        if(bool){
            return "success";
        }
        return "failure";
    }

    /**
     * delete
     * @return
     */
    @RequestMapping(value="/sys/delete.action",produces="application/text;charset=UTF-8")
    @ResponseBody
    public String delete(String ids) throws Exception{
       boolean bool= sysServer.deleteSyss(ids);
       if(bool){
            return "success";
       }
       return "failure";
    }
}